/**
 * 
 */
package ve.com.neometrica.inventario.impl.dao;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ve.com.neometrica.inventario.api.dao.InventarioDao;
import ve.com.neometrica.inventario.api.model.Inventario;

/**
 * @author a.cabrera
 *
 */
public class InventarioDaoImpl extends HibernateDaoSupport implements
		InventarioDao {

	private SimpleDateFormat formatDDMMYYYY=new SimpleDateFormat("dd/MM/yyyy");
    private SimpleDateFormat formatDDMMYYYYHHmm=new SimpleDateFormat("dd/MM/yyyy HH:mm");
	/* (non-Javadoc)
	 * @see ve.com.neometrica.inventario.api.dao.InventarioDao#save(ve.com.neometrica.inventario.api.model.Inventario)
	 */
	@Override
	public void save(Inventario inventario) {
		if(inventario.getId()!=null){
			this.getHibernateTemplate().saveOrUpdate(inventario);
		}else{
			this.getHibernateTemplate().save(inventario);
		}
	}

	/* (non-Javadoc)
	 * @see ve.com.neometrica.inventario.api.dao.InventarioDao#getAll()
	 */
	@Override
	public List<Inventario> getAll() {
		return getHibernateTemplate().loadAll(Inventario.class);
	}

	/* (non-Javadoc)
	 * @see ve.com.neometrica.inventario.api.dao.InventarioDao#getById(java.lang.Integer)
	 */
	@Override
	public Inventario getById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public List<Inventario> getInventariosConsulta() {
		List<Inventario> inventarios=new LinkedList<Inventario>();
		Query q=getSession().getNamedQuery("getInventariosConsulta");
		List<Object>l=q.list();
		if(l.size()>0){
			for (Object object : l) {
				Object[] o=(Object[])object;
				Inventario i=null;
				try{
				i=new Inventario(Integer.valueOf(String.valueOf(o[0])),
								o[1]!=null?Integer.valueOf(String.valueOf(o[1])):new Integer(0),
								o[2]!=null?BigDecimal.valueOf(((Number)o[2]).doubleValue()):BigDecimal.valueOf(0),
								o[3]!=null?formatDDMMYYYYHHmm.parse(String.valueOf(o[3])):null,		
								o[4]!=null?formatDDMMYYYYHHmm.parse(String.valueOf(o[4])):null,
								o[5]!=null?Integer.valueOf(String.valueOf(o[5])):new Integer(0),
								o[6]!=null?String.valueOf(o[6]):"",
								o[7]!=null?Integer.valueOf(String.valueOf(o[7])):new Integer(0),
								o[8]!=null?String.valueOf(o[8]):""		
								);
				inventarios.add(i);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return inventarios;
	}

}
